﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views;
using COMOSOFT.Class;
using DevExpress.XtraGrid.Views.Layout;
using System.IO;
using COMOSOFT.HumanResource.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;

namespace COMOSOFT.JobManagement.USC
{
    public partial class UscStaffInfo : UserControl
    {
        public UscStaffInfo()
        {
            InitializeComponent();
        }
        Task task;
        DataTable dbTemp = null;
        ClassShare cls = new ClassShare("UscStaffInfo");
        Image img = null;
        FrmPreview frmPV = null;
        private void layoutView_StaffInfo_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {           
            DataRowView row = e.Row as DataRowView;
            LayoutView view = sender as LayoutView;            
            if (e.Column.FieldName == "colImage" && e.IsGetData)
                e.Value = LoadImg(row["EID"].ToString(), e.ListSourceRowIndex);

        }

        private void UscStaffInfo_Load(object sender, EventArgs e)
        {
        }

        public void LoadList()
        {
            if (task == null || task.Status != TaskStatus.Running)
            {
                splashScreenManager1.ShowWaitForm();
                gridControl_StaffInfo.DataSource = null;
                task = Task.Factory.StartNew(() => LoadData());
                task.ContinueWith(tt => FillTable());
            }
        }
        private delegate void dlgFillTable();
        private void FillTable()
        {
            if (InvokeRequired)
            {
                this.Invoke(new dlgFillTable(FillTable));
            }
            else
            {
                gridControl_StaffInfo.DataSource=dataSetOrderManagement.Employee;
                splashScreenManager1.CloseWaitForm();
                //layoutView_StaffInfo.FocusedRowHandle = layoutView_StaffInfo.RowCount/2;
            }
        }

        #region [Show Image]
        private Image LoadImg(string ID, int row)
        {
            try
            {
                if (dbTemp.Rows.Count > row && dbTemp.Rows[row]["ID"].ToString() == ID)
                {
                    using (System.IO.MemoryStream str = new System.IO.MemoryStream(dbTemp.Rows[row]["Img"] as byte[]))
                    {
                        img = Image.FromStream(str);
                        return img;
                    }
                }
            }
            catch
            {
                //ssageBox.Show(ex.Message);
            }
            return img;
        }
        #endregion

        private void LoadData()
        {
            employeeTableAdapter.Fill(dataSetOrderManagement.Employee);
            dbTemp = new DataTable();
            dbTemp.Columns.Add("ID");
            dbTemp.Columns.Add("Img", typeof(object));
            DataTable dbMA = dataSetOrderManagement.Employee;
            for (int i = 0; i < dbMA.Rows.Count; i++)
            {
                DataRow newrow = dbTemp.NewRow();
                if (dbMA.Rows[i]["Image"].ToString() != "")
                {
                    if (!File.Exists(ClassShare.lcSystem + dbMA.Rows[i]["Image"].ToString()))
                        cls.Dowload(ClassShare.ftpSystem, ClassShare.lcSystem, dbMA.Rows[i]["Image"].ToString());
                    string path = ClassShare.lcSystem + dbMA.Rows[i]["Image"].ToString();
                    byte[] img = System.IO.File.ReadAllBytes(path);
                    newrow["Img"] = img;
                }
                else
                {
                    if (!File.Exists(ClassShare.lcMaterial + "noImage.gif"))
                        cls.Dowload(ClassShare.ftpMaterial, ClassShare.lcMaterial, "noImage.gif");
                    string path = ClassShare.lcMaterial + "noImage.gif";
                    byte[] NoImg = System.IO.File.ReadAllBytes(path);
                    newrow["Img"] = NoImg;
                }
                newrow["ID"] = dbMA.Rows[i]["EID"];
                dbTemp.Rows.Add(newrow);
            }               
        }
        public void SelectItem(int index)
        {
            try
            {
                layoutView_StaffInfo.FocusedRowHandle = index;
            }
            catch
            {
            }
        }

        #region[Update Employee]
        private void layoutView_StaffInfo_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            try
            {
                employeeBindingSource.EndEdit();
                employeeTableAdapter.Update(dataSetOrderManagement.Employee);
                dataSetOrderManagement.Employee.AcceptChanges();
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        public void Preview()
        {
            //GridView view = layoutView_StaffInfo as GridView;
            //frmPV = new FrmPreview(view);
            //frmPV.LoadData();
            //frmPV.Show();
        }
    }
}
